Genetic optimization of PID parameters for empyrically identified system

Albertas Mickenas mic@wemakethings.net, 2013 copyleft, whatever

In [1]:
cd ~/Xaltura/pidgenetics/


/home/miceuz/Xaltura/pidgenetics

In [2]:
import scipy.signal as signal
import numpy as np
import matplotlib.pyplot as plt
import pylab as pylab
import math as math
import pid

In [3]:
#utility
def idx(x):
    return range(0, x.shape[0])

In [4]:
#with pylab inline, make bigger images
pylab.rcParams['figure.figsize'] = (10.0, 8.0)

The plant


In [5]:
from PIL import Image
im = Image.open("plant.jpg")
f = plt.figure()
plt.imshow(im.transpose(Image.FLIP_TOP_BOTTOM))


Out[5]:
<matplotlib.image.AxesImage at 0x3a09190>

System identification

Load data and plot step response

Step response is a reaction of a system to the maximum signal level at its input. In my case this was PWM signal on a microcontroller. I've chosen my maximum to be 128 as it keeps the resistor from smoking and leaves the system a bit of hump behind it if PID algorithm chooses to overdrive the input.

In [6]:
y = np.loadtxt("step-response.txt")
#normalize it
y = y - min(y)
y = y / max(y)
#plot it
plt.plot(idx(y), y, color="blue", label="Plant step response")
plt.legend()


Out[6]:
<matplotlib.legend.Legend at 0x3a096d0>

Impulse response is a derivative of the step response


In [7]:
#impulse response is a derivative of step response
ir = np.diff(y)

n = 33
window = signal.firwin(n, cutoff = 0.1, window = "blackman")

irFiltered = signal.lfilter(window, 1, ir)[16:]
plt.plot(idx(ir), ir, color="grey", label="Plant impulse response")
plt.plot(idx(irFiltered), irFiltered, color="red", label="filtered")
plt.legend()


Out[7]:
<matplotlib.legend.Legend at 0x3811590>
Test the impulse response by convolving it with dirac delta and compare it to our step response. Convolving with direac delta means applying full power to a system. Impulse response is everything there is to be known about the system. It fully describes the whole possible behaviour of the system (provided some some limitations - system has to be linear for this to work). To predict the behaviour of the system, for every discrete input value you have to multiply impulse response by dirac delta, scaled to input value, to get the ouptut; and then on the next input, shift the previous output to right and add new output on the top, so impulse responses overlap on top of each other by one while time passes. This is called convolution. Pretty good hm? Simple math at work.

In [8]:
#dirty global output variable is born here
output = np.zeros(len(y)*2)

#convolve impuse reponse with 1. 1 * ir = y
for i in idx(y):
    for j in idx(ir):
        output[i+j] = output[i+j] + 1 * ir[j]
plt.plot(idx(y), y, color="blue", label="Plant step response")
plt.plot(idx(output[:len(y)]), output[:len(y)], color="red", label="Simulated step response")


Out[8]:
[<matplotlib.lines.Line2D at 0x3bcf590>]

Modelling

This function processes one control step.


In [9]:
#note the ugly use of global while i can allow this myself in this messy example

def processStep(t, scaling):
    for j in idx(irFiltered):
        output[t+j] = output[t+j] + irFiltered[j] * scaling

This one evaluates how good the controller is


In [59]:
def evaluate(p, setpoint, plot=False):
    global output
    ITAE = 0
    MSE = 0
    IAE = 0
    output = np.zeros(150 + len(ir))
    controls = np.zeros(150)
    p.setPoint(setpoint)
    control = 0
    for t in range(0, 150):
        controls[t] = control
        processStep(t, control)
        control = p.update(output[t])
        error = output[t] - setpoint
        ITAE = ITAE + t * abs(error)
        MSE = MSE + error**2
        IAE = IAE + abs(error)
    output = output[:-len(ir)]
    MSE = MSE / 150
    
    if plot:
        print "ITAE:", ITAE, "MSE:", MSE, "IAE", IAE, "PID: [" , str(p.Kp), ", ", str(p.Ki), ", ", str(p.Kd), "]"
        errors = output - setpoint
        #plt.figure()
        #plt.plot(idx(errors), abs(errors), color="red", label="Ouput error")
        #plt.title("PID: [" + str(p.Kp) + ", " + str(p.Ki) + ", " + str(p.Kd) + "] errors")
        #plt.legend()
        plt.figure()
        plt.plot(idx(output), np.ones(len(output))*setpoint, color="red", label="Setpoint")    
        plt.plot(idx(controls), controls, color="grey", label="Control signal")
        plt.plot(idx(output), output, color="blue", label="PID controllable model output")
        plt.legend()
        plt.title("ITAE: " + str(ITAE) + "MSE:" + str(MSE) + "IAE" + str(IAE)+ " PID: [" + str(p.Kp) + ", " + str(p.Ki) + ", " + str(p.Kd) + "]")
        
    return ITAE

It's alive!


In [60]:
POPULATION = 9
GENERATIONS = 100
MUTATION_PROBABILITY = 0.5
MUTATION_SCALE = 0.05
MAX_P = 20
MAX_I = 20#0.08
MAX_D = 22
population = []

Mutation and crossbreeding algorithms


In [61]:
def mutation(maxMutation):
    if rand() < MUTATION_PROBABILITY:
        return (0.5 - rand()) * maxMutation
    else:
        return 0

def nextGeneration(population):
    population.sort(key=lambda individual: individual.getFitness())
    population = population[:-POPULATION/3*2] #part of population dies
 
    alpha = population[0]
    #alpha female mates with the best without mutation
    for i in range(0, POPULATION/3 - 1):
        mama = alpha
        papa = population[int(rand() * len(population))]
        t = rand()
        if 0 < t <= 0.33:
            P = papa.Kp# + mutation(MAX_P * MUTATION_SCALE)
            I = mama.Ki# + mutation(MAX_I * MUTATION_SCALE)
            D = mama.Kd# + mutation(MAX_D * MUTATION_SCALE)
        elif 0.33 < t <= 0.66:
            P = mama.Kp# + mutation(MAX_P * MUTATION_SCALE)
            I = papa.Ki# + mutation(MAX_I * MUTATION_SCALE)
            D = mama.Kd# + mutation(MAX_D * MUTATION_SCALE)
        else:
            P = mama.Kp# + mutation(MAX_P * MUTATION_SCALE)
            I = mama.Ki# + mutation(MAX_I * MUTATION_SCALE)
            D = papa.Kd# + mutation(MAX_D * MUTATION_SCALE)
        population.append(pid.PID(P, I, D))


    #all the other mate randomly and mutate a bit
    for i in range(0, POPULATION/3 - 1):
        papa = population[int(rand() * len(population))]
        mama = population[int(rand() * len(population))]
        t = rand()
        if 0 < t <= 0.33:
            P = papa.Kp + mutation(MAX_P * MUTATION_SCALE)
            I = mama.Ki + mutation(MAX_I * MUTATION_SCALE)
            D = mama.Kd + mutation(MAX_D * MUTATION_SCALE)
        elif 0.33 < t <= 0.66:
            P = mama.Kp + mutation(MAX_P * MUTATION_SCALE)
            I = papa.Ki + mutation(MAX_I * MUTATION_SCALE)
            D = mama.Kd + mutation(MAX_D * MUTATION_SCALE)
        else:
            P = mama.Kp + mutation(MAX_P * MUTATION_SCALE)
            I = mama.Ki + mutation(MAX_I * MUTATION_SCALE)
            D = papa.Kd + mutation(MAX_D * MUTATION_SCALE)
        population.append(pid.PID(P, I, D))
    
    #just for the kicks add a ramdom one
    population.append(pid.PID(rand() * MAX_P, rand() * MAX_I, rand() * MAX_D))
    
    #and one mutated of personally alpha
    P = alpha.Kp + mutation(MAX_P * MUTATION_SCALE)
    I = alpha.Ki + mutation(MAX_I * MUTATION_SCALE)
    D = alpha.Kd + mutation(MAX_D * MUTATION_SCALE)
    population.append(pid.PID(P, I, D))
        
    return population

Let it live


In [62]:
#utility
def evaluateFitness(population):
    #print "individual evaluations of population: [",
    for individual in population:
        if -1 == individual.getFitness():
            f = evaluate(individual, 0.5)
            individual.setFitness(f)
    #    print individual.getFitness(),
    #print "]"
    population.sort(key=lambda individual: individual.getFitness())
    

for i in range(0, POPULATION):
    population.append(pid.PID(rand() * MAX_P, rand() * MAX_I, rand() * MAX_D))

for g in range(0, GENERATIONS):
    evaluateFitness(population);
    print "Generation:", g, "Alpha individual fitness:", population[0].getFitness()
    evaluate(population[0], setpoint = 0.5, plot = True)
    population = nextGeneration(population)


Generation: 0 Alpha individual fitness: 0.056884639039
ITAE: 2076.46744736 MSE: 0.056884639039 IAE 30.7882531909 PID: [ 13.3218126393 ,  1.54095270455 ,  7.21332292795 ]
Generation: 1 Alpha individual fitness: 0.0496627057897
ITAE: 1618.92472118 MSE: 0.0496627057897 IAE 27.3519638799 PID: [ 13.3218126393 ,  0.682058560901 ,  7.21332292795 ]
Generation: 2 Alpha individual fitness: 0.0496603450277
ITAE: 1618.73970731 MSE: 0.0496603450277 IAE 27.35055272 PID: [ 13.3218126393 ,  0.682058560901 ,  7.57981006041 ]
Generation: 3 Alpha individual fitness: 0.0496603450277
ITAE: 1618.73970731 MSE: 0.0496603450277 IAE 27.35055272 PID: [ 13.3218126393 ,  0.682058560901 ,  7.57981006041 ]
Generation: 4 Alpha individual fitness: 0.0480391511495
ITAE: 1503.68969339 MSE: 0.0480391511495 IAE 26.4513619705 PID: [ 13.3218126393 ,  0.593249838994 ,  8.10768560158 ]
Generation: 5 Alpha individual fitness: 0.04412288051
ITAE: 1368.26641086 MSE: 0.04412288051 IAE 25.1610231983 PID: [ 13.3218126393 ,  0.405218102588 ,  8.41834564023 ]
Generation: 6 Alpha individual fitness: 0.0441210090014
ITAE: 1368.0832679 MSE: 0.0441210090014 IAE 25.1594456561 PID: [ 13.3218126393 ,  0.405218102588 ,  8.10768560158 ]
Generation: 7 Alpha individual fitness: 0.0408371380323
ITAE: 1212.53855152 MSE: 0.0408371380323 IAE 23.6697879543 PID: [ 13.4694503769 ,  0.283922780019 ,  8.10768560158 ]
Generation: 8 Alpha individual fitness: 0.0408354587217
ITAE: 1212.37314726 MSE: 0.0408354587217 IAE 23.6683052352 PID: [ 13.4694503769 ,  0.283922780019 ,  7.71889656214 ]
Generation: 9 Alpha individual fitness: 0.0337387579836
ITAE: 600.947243973 MSE: 0.0337387579836 IAE 17.5829739293 PID: [ 13.9588144572 ,  0.0932305059083 ,  7.87671964282 ]
Generation: 10 Alpha individual fitness: 0.0329247267696
ITAE: 416.719637863 MSE: 0.0329247267696 IAE 15.6278876371 PID: [ 13.9588144572 ,  0.0157215996096 ,  7.53655853749 ]
Generation: 11 Alpha individual fitness: 0.0329247267696
ITAE: 416.719637863 MSE: 0.0329247267696 IAE 15.6278876371 PID: [ 13.9588144572 ,  0.0157215996096 ,  7.53655853749 ]
Generation: 12 Alpha individual fitness: 0.0327134182599
ITAE: 263.405296575 MSE: 0.0327134182599 IAE 14.2194746531 PID: [ 13.9588144572 ,  0.0393987276882 ,  7.53655853749 ]
Generation: 13 Alpha individual fitness: 0.0327134182599
ITAE: 263.405296575 MSE: 0.0327134182599 IAE 14.2194746531 PID: [ 13.9588144572 ,  0.0393987276882 ,  7.53655853749 ]
Generation: 14 Alpha individual fitness: 0.0327131475472
ITAE: 263.363472067 MSE: 0.0327131475472 IAE 14.2181550411 PID: [ 13.6334155996 ,  0.0393987276882 ,  7.78196915631 ]
Generation: 15 Alpha individual fitness: 0.0327131033564
ITAE: 263.240561912 MSE: 0.0327131033564 IAE 14.2170506535 PID: [ 13.8810978071 ,  0.0393987276882 ,  7.88683497877 ]
Generation: 16 Alpha individual fitness: 0.0327131033564
ITAE: 263.240561912 MSE: 0.0327131033564 IAE 14.2170506535 PID: [ 13.8810978071 ,  0.0393987276882 ,  7.88683497877 ]
Generation: 17 Alpha individual fitness: 0.0327131033564
ITAE: 263.240561912 MSE: 0.0327131033564 IAE 14.2170506535 PID: [ 13.8810978071 ,  0.0393987276882 ,  7.88683497877 ]
Generation: 18 Alpha individual fitness: 0.0327131033564
ITAE: 263.240561912 MSE: 0.0327131033564 IAE 14.2170506535 PID: [ 13.8810978071 ,  0.0393987276882 ,  7.88683497877 ]
Generation: 19 Alpha individual fitness: 0.0327131033564
ITAE: 263.240561912 MSE: 0.0327131033564 IAE 14.2170506535 PID: [ 13.8810978071 ,  0.0393987276882 ,  7.88683497877 ]
Generation: 20 Alpha individual fitness: 0.0327130244768
ITAE: 263.192174528 MSE: 0.0327130244768 IAE 14.2163899895 PID: [ 13.8810978071 ,  0.0393987276882 ,  7.97970796846 ]
Generation: 21 Alpha individual fitness: 0.0327130244768
ITAE: 263.192174528 MSE: 0.0327130244768 IAE 14.2163899895 PID: [ 13.8810978071 ,  0.0393987276882 ,  7.97970796846 ]
Generation: 22 Alpha individual fitness: 0.0327130244768
ITAE: 263.192174528 MSE: 0.0327130244768 IAE 14.2163899895 PID: [ 13.8810978071 ,  0.0393987276882 ,  7.97970796846 ]
Generation: 23 Alpha individual fitness: 0.0327129192788
ITAE: 263.127537185 MSE: 0.0327129192788 IAE 14.2155071259 PID: [ 13.8810978071 ,  0.0393987276882 ,  8.1041517369 ]
Generation: 24 Alpha individual fitness: 0.0327129192788
ITAE: 263.127537185 MSE: 0.0327129192788 IAE 14.2155071259 PID: [ 13.8810978071 ,  0.0393987276882 ,  8.1041517369 ]
Generation: 25 Alpha individual fitness: 0.0327126153037
ITAE: 262.940080156 MSE: 0.0327126153037 IAE 14.2129445656 PID: [ 13.8810978071 ,  0.0393987276882 ,  8.46754605768 ]
Generation: 26 Alpha individual fitness: 0.0327125295921
ITAE: 263.105445316 MSE: 0.0327125295921 IAE 14.2140500951 PID: [ 13.4078416301 ,  0.0393987276882 ,  8.46754605768 ]
Generation: 27 Alpha individual fitness: 0.032712333722
ITAE: 263.198559638 MSE: 0.032712333722 IAE 14.2141158014 PID: [ 12.9463559783 ,  0.0393987276882 ,  8.46754605768 ]
Generation: 28 Alpha individual fitness: 0.032712333722
ITAE: 263.198559638 MSE: 0.032712333722 IAE 14.2141158014 PID: [ 12.9463559783 ,  0.0393987276882 ,  8.46754605768 ]
Generation: 29 Alpha individual fitness: 0.032712333722
ITAE: 263.198559638 MSE: 0.032712333722 IAE 14.2141158014 PID: [ 12.9463559783 ,  0.0393987276882 ,  8.46754605768 ]
Generation: 30 Alpha individual fitness: 0.0327119271134
ITAE: 262.949304483 MSE: 0.0327119271134 IAE 14.2106618476 PID: [ 12.9463559783 ,  0.0393987276882 ,  8.93184499395 ]
Generation: 31 Alpha individual fitness: 0.0327119271134
ITAE: 262.949304483 MSE: 0.0327119271134 IAE 14.2106618476 PID: [ 12.9463559783 ,  0.0393987276882 ,  8.93184499395 ]
Generation: 32 Alpha individual fitness: 0.0327119271134
ITAE: 262.949304483 MSE: 0.0327119271134 IAE 14.2106618476 PID: [ 12.9463559783 ,  0.0393987276882 ,  8.93184499395 ]
Generation: 33 Alpha individual fitness: 0.0327119271134
ITAE: 262.949304483 MSE: 0.0327119271134 IAE 14.2106618476 PID: [ 12.9463559783 ,  0.0393987276882 ,  8.93184499395 ]
Generation: 34 Alpha individual fitness: 0.0327117093742
ITAE: 263.048463075 MSE: 0.0327117093742 IAE 14.2106279654 PID: [ 12.4686006091 ,  0.0393987276882 ,  8.93184499395 ]
Generation: 35 Alpha individual fitness: 0.0327111679189
ITAE: 262.917180422 MSE: 0.0327111679189 IAE 14.2074519999 PID: [ 12.0162007682 ,  0.0393987276882 ,  9.33492279451 ]
Generation: 36 Alpha individual fitness: 0.0327111679189
ITAE: 262.917180422 MSE: 0.0327111679189 IAE 14.2074519999 PID: [ 12.0162007682 ,  0.0393987276882 ,  9.33492279451 ]
Generation: 37 Alpha individual fitness: 0.0327111679189
ITAE: 262.917180422 MSE: 0.0327111679189 IAE 14.2074519999 PID: [ 12.0162007682 ,  0.0393987276882 ,  9.33492279451 ]
Generation: 38 Alpha individual fitness: 0.0327111679189
ITAE: 262.917180422 MSE: 0.0327111679189 IAE 14.2074519999 PID: [ 12.0162007682 ,  0.0393987276882 ,  9.33492279451 ]
Generation: 39 Alpha individual fitness: 0.0327111679189
ITAE: 262.917180422 MSE: 0.0327111679189 IAE 14.2074519999 PID: [ 12.0162007682 ,  0.0393987276882 ,  9.33492279451 ]
Generation: 40 Alpha individual fitness: 0.0327108469252
ITAE: 262.71476152 MSE: 0.0327108469252 IAE 14.2046145237 PID: [ 12.0162007682 ,  0.0393987276882 ,  9.72185822413 ]
Generation: 41 Alpha individual fitness: 0.0327108469252
ITAE: 262.71476152 MSE: 0.0327108469252 IAE 14.2046145237 PID: [ 12.0162007682 ,  0.0393987276882 ,  9.72185822413 ]
Generation: 42 Alpha individual fitness: 0.0327108469252
ITAE: 262.71476152 MSE: 0.0327108469252 IAE 14.2046145237 PID: [ 12.0162007682 ,  0.0393987276882 ,  9.72185822413 ]
Generation: 43 Alpha individual fitness: 0.0327108304965
ITAE: 262.721522024 MSE: 0.0327108304965 IAE 14.2045846862 PID: [ 11.9776091058 ,  0.0393987276882 ,  9.72185822413 ]
Generation: 44 Alpha individual fitness: 0.0327106715141
ITAE: 262.808536604 MSE: 0.0327106715141 IAE 14.2045274544 PID: [ 11.5979708859 ,  0.0393987276882 ,  9.72185822413 ]
Generation: 45 Alpha individual fitness: 0.032710544154
ITAE: 262.854044633 MSE: 0.032710544154 IAE 14.2042394129 PID: [ 11.3333533914 ,  0.0393987276882 ,  9.72185822413 ]
Generation: 46 Alpha individual fitness: 0.032710272365
ITAE: 262.788998202 MSE: 0.032710272365 IAE 14.2022377687 PID: [ 11.0378575305 ,  0.0393987276882 ,  9.84682658898 ]
Generation: 47 Alpha individual fitness: 0.032710272365
ITAE: 262.788998202 MSE: 0.032710272365 IAE 14.2022377687 PID: [ 11.0378575305 ,  0.0393987276882 ,  9.84682658898 ]
Generation: 48 Alpha individual fitness: 0.032710272365
ITAE: 262.788998202 MSE: 0.032710272365 IAE 14.2022377687 PID: [ 11.0378575305 ,  0.0393987276882 ,  9.84682658898 ]
Generation: 49 Alpha individual fitness: 0.032710272365
ITAE: 262.788998202 MSE: 0.032710272365 IAE 14.2022377687 PID: [ 11.0378575305 ,  0.0393987276882 ,  9.84682658898 ]
Generation: 50 Alpha individual fitness: 0.032710272365
ITAE: 262.788998202 MSE: 0.032710272365 IAE 14.2022377687 PID: [ 11.0378575305 ,  0.0393987276882 ,  9.84682658898 ]
Generation: 51 Alpha individual fitness: 0.032710272365
ITAE: 262.788998202 MSE: 0.032710272365 IAE 14.2022377687 PID: [ 11.0378575305 ,  0.0393987276882 ,  9.84682658898 ]
Generation: 52 Alpha individual fitness: 0.0327102542006
ITAE: 262.792424336 MSE: 0.0327102542006 IAE 14.2021610321 PID: [ 11.0052161708 ,  0.0393987276882 ,  9.84682658898 ]
Generation: 53 Alpha individual fitness: 0.0327102542006
ITAE: 262.792424336 MSE: 0.0327102542006 IAE 14.2021610321 PID: [ 11.0052161708 ,  0.0393987276882 ,  9.84682658898 ]
Generation: 54 Alpha individual fitness: 0.0327100782286
ITAE: 262.680063326 MSE: 0.0327100782286 IAE 14.2005548673 PID: [ 11.0052161708 ,  0.0393987276882 ,  10.0622637244 ]
Generation: 55 Alpha individual fitness: 0.032709597799
ITAE: 262.510503192 MSE: 0.032709597799 IAE 14.1969225204 PID: [ 10.7197663816 ,  0.0393987276882 ,  10.47499338 ]
Generation: 56 Alpha individual fitness: 0.032709597799
ITAE: 262.510503192 MSE: 0.032709597799 IAE 14.1969225204 PID: [ 10.7197663816 ,  0.0393987276882 ,  10.47499338 ]
Generation: 57 Alpha individual fitness: 0.0327095349149
ITAE: 262.522795569 MSE: 0.0327095349149 IAE 14.1965691796 PID: [ 10.5964782614 ,  0.0393987276882 ,  10.47499338 ]
Generation: 58 Alpha individual fitness: 0.0327095349149
ITAE: 262.522795569 MSE: 0.0327095349149 IAE 14.1965691796 PID: [ 10.5964782614 ,  0.0393987276882 ,  10.47499338 ]
Generation: 59 Alpha individual fitness: 0.0327095349149
ITAE: 262.522795569 MSE: 0.0327095349149 IAE 14.1965691796 PID: [ 10.5964782614 ,  0.0393987276882 ,  10.47499338 ]
Generation: 60 Alpha individual fitness: 0.0327095349149
ITAE: 262.522795569 MSE: 0.0327095349149 IAE 14.1965691796 PID: [ 10.5964782614 ,  0.0393987276882 ,  10.47499338 ]
Generation: 61 Alpha individual fitness: 0.0327095349149
ITAE: 262.522795569 MSE: 0.0327095349149 IAE 14.1965691796 PID: [ 10.5964782614 ,  0.0393987276882 ,  10.47499338 ]
Generation: 62 Alpha individual fitness: 0.0327095349149
ITAE: 262.522795569 MSE: 0.0327095349149 IAE 14.1965691796 PID: [ 10.5964782614 ,  0.0393987276882 ,  10.47499338 ]
Generation: 63 Alpha individual fitness: 0.0327095349149
ITAE: 262.522795569 MSE: 0.0327095349149 IAE 14.1965691796 PID: [ 10.5964782614 ,  0.0393987276882 ,  10.47499338 ]
Generation: 64 Alpha individual fitness: 0.0327094358861
ITAE: 262.538723064 MSE: 0.0327094358861 IAE 14.1959319187 PID: [ 10.3979172091 ,  0.0393987276882 ,  10.47499338 ]
Generation: 65 Alpha individual fitness: 0.0327094358861
ITAE: 262.538723064 MSE: 0.0327094358861 IAE 14.1959319187 PID: [ 10.3979172091 ,  0.0393987276882 ,  10.47499338 ]
Generation: 66 Alpha individual fitness: 0.0327094135554
ITAE: 262.547767405 MSE: 0.0327094135554 IAE 14.195851283 PID: [ 10.3523263969 ,  0.0393987276882 ,  10.47499338 ]
Generation: 67 Alpha individual fitness: 0.0327092958018
ITAE: 262.305753859 MSE: 0.0327092958018 IAE 14.1940000425 PID: [ 10.722328759 ,  0.0393987276882 ,  10.8675813728 ]
Generation: 68 Alpha individual fitness: 0.0327089872723
ITAE: 262.387782421 MSE: 0.0327089872723 IAE 14.1922357183 PID: [ 10.0740677779 ,  0.0393987276882 ,  10.8675813728 ]
Generation: 69 Alpha individual fitness: 0.0327087985459
ITAE: 262.255642458 MSE: 0.0327087985459 IAE 14.1903150395 PID: [ 10.0740677779 ,  0.0393987276882 ,  11.1266127752 ]
Generation: 70 Alpha individual fitness: 0.0327087985459
ITAE: 262.255642458 MSE: 0.0327087985459 IAE 14.1903150395 PID: [ 10.0740677779 ,  0.0393987276882 ,  11.1266127752 ]
Generation: 71 Alpha individual fitness: 0.0327087015481
ITAE: 262.18537031 MSE: 0.0327087015481 IAE 14.1892899242 PID: [ 10.0740677779 ,  0.0393987276882 ,  11.2629395871 ]
Generation: 72 Alpha individual fitness: 0.0327087015481
ITAE: 262.18537031 MSE: 0.0327087015481 IAE 14.1892899242 PID: [ 10.0740677779 ,  0.0393987276882 ,  11.2629395871 ]
Generation: 73 Alpha individual fitness: 0.0327087015481
ITAE: 262.18537031 MSE: 0.0327087015481 IAE 14.1892899242 PID: [ 10.0740677779 ,  0.0393987276882 ,  11.2629395871 ]
Generation: 74 Alpha individual fitness: 0.0327087015481
ITAE: 262.18537031 MSE: 0.0327087015481 IAE 14.1892899242 PID: [ 10.0740677779 ,  0.0393987276882 ,  11.2629395871 ]
Generation: 75 Alpha individual fitness: 0.0327083537451
ITAE: 261.929778694 MSE: 0.0327083537451 IAE 14.1855562178 PID: [ 10.0740677779 ,  0.0393987276882 ,  11.7635770393 ]
Generation: 76 Alpha individual fitness: 0.0327083537451
ITAE: 261.929778694 MSE: 0.0327083537451 IAE 14.1855562178 PID: [ 10.0740677779 ,  0.0393987276882 ,  11.7635770393 ]
Generation: 77 Alpha individual fitness: 0.0327080675152
ITAE: 261.863432977 MSE: 0.0327080675152 IAE 14.1828368166 PID: [ 9.73229231505 ,  0.0393987276882 ,  12.0514127402 ]
Generation: 78 Alpha individual fitness: 0.0327080675152
ITAE: 261.863432977 MSE: 0.0327080675152 IAE 14.1828368166 PID: [ 9.73229231505 ,  0.0393987276882 ,  12.0514127402 ]
Generation: 79 Alpha individual fitness: 0.0327080675152
ITAE: 261.863432977 MSE: 0.0327080675152 IAE 14.1828368166 PID: [ 9.73229231505 ,  0.0393987276882 ,  12.0514127402 ]
Generation: 80 Alpha individual fitness: 0.0327078557141
ITAE: 261.581048463 MSE: 0.0327078557141 IAE 14.1801700535 PID: [ 10.0191716165 ,  0.0393987276882 ,  12.4931133747 ]
Generation: 81 Alpha individual fitness: 0.0327077807695
ITAE: 261.674488734 MSE: 0.0327077807695 IAE 14.1798734292 PID: [ 9.69208453989 ,  0.0393987276882 ,  12.4931133747 ]
Generation: 82 Alpha individual fitness: 0.0327077807695
ITAE: 261.674488734 MSE: 0.0327077807695 IAE 14.1798734292 PID: [ 9.69208453989 ,  0.0393987276882 ,  12.4931133747 ]
Generation: 83 Alpha individual fitness: 0.0327077807695
ITAE: 261.674488734 MSE: 0.0327077807695 IAE 14.1798734292 PID: [ 9.69208453989 ,  0.0393987276882 ,  12.4931133747 ]
Generation: 84 Alpha individual fitness: 0.0327077807695
ITAE: 261.674488734 MSE: 0.0327077807695 IAE 14.1798734292 PID: [ 9.69208453989 ,  0.0393987276882 ,  12.4931133747 ]
Generation: 85 Alpha individual fitness: 0.0327075322679
ITAE: 261.492613187 MSE: 0.0327075322679 IAE 14.1772121989 PID: [ 9.69208453989 ,  0.0393987276882 ,  12.9030433513 ]
Generation: 86 Alpha individual fitness: 0.0327075160383
ITAE: 261.480602117 MSE: 0.0327075160383 IAE 14.1770362617 PID: [ 9.69208453989 ,  0.0393987276882 ,  12.9303053308 ]
Generation: 87 Alpha individual fitness: 0.0327074802995
ITAE: 261.5746779 MSE: 0.0327074802995 IAE 14.1769835282 PID: [ 9.44071817367 ,  0.0393987276882 ,  12.9303053308 ]
Generation: 88 Alpha individual fitness: 0.0327074802995
ITAE: 261.5746779 MSE: 0.0327074802995 IAE 14.1769835282 PID: [ 9.44071817367 ,  0.0393987276882 ,  12.9303053308 ]
Generation: 89 Alpha individual fitness: 0.0327074734485
ITAE: 261.734630068 MSE: 0.0327074734485 IAE 14.1769677695 PID: [ 9.06280200185 ,  0.0393987276882 ,  12.9303053308 ]
Generation: 90 Alpha individual fitness: 0.0327072889431
ITAE: 261.579495199 MSE: 0.0327072889431 IAE 14.174611377 PID: [ 9.06280200185 ,  0.0393987276882 ,  13.3226824856 ]
Generation: 91 Alpha individual fitness: 0.0327072889431
ITAE: 261.579495199 MSE: 0.0327072889431 IAE 14.174611377 PID: [ 9.06280200185 ,  0.0393987276882 ,  13.3226824856 ]
Generation: 92 Alpha individual fitness: 0.0327072889431
ITAE: 261.579495199 MSE: 0.0327072889431 IAE 14.174611377 PID: [ 9.06280200185 ,  0.0393987276882 ,  13.3226824856 ]
Generation: 93 Alpha individual fitness: 0.0327072889431
ITAE: 261.579495199 MSE: 0.0327072889431 IAE 14.174611377 PID: [ 9.06280200185 ,  0.0393987276882 ,  13.3226824856 ]
Generation: 94 Alpha individual fitness: 0.0327072448132
ITAE: 261.500599839 MSE: 0.0327072448132 IAE 14.174554019 PID: [ 9.22876398797 ,  0.0393987276882 ,  13.3226824856 ]
Generation: 95 Alpha individual fitness: 0.0327072448132
ITAE: 261.500599839 MSE: 0.0327072448132 IAE 14.174554019 PID: [ 9.22876398797 ,  0.0393987276882 ,  13.3226824856 ]
Generation: 96 Alpha individual fitness: 0.0327072448132
ITAE: 261.500599839 MSE: 0.0327072448132 IAE 14.174554019 PID: [ 9.22876398797 ,  0.0393987276882 ,  13.3226824856 ]
Generation: 97 Alpha individual fitness: 0.0327072448132
ITAE: 261.500599839 MSE: 0.0327072448132 IAE 14.174554019 PID: [ 9.22876398797 ,  0.0393987276882 ,  13.3226824856 ]
Generation: 98 Alpha individual fitness: 0.0327072448132
ITAE: 261.500599839 MSE: 0.0327072448132 IAE 14.174554019 PID: [ 9.22876398797 ,  0.0393987276882 ,  13.3226824856 ]
Generation: 99 Alpha individual fitness: 0.0327072448132
ITAE: 261.500599839 MSE: 0.0327072448132 IAE 14.174554019 PID: [ 9.22876398797 ,  0.0393987276882 ,  13.3226824856 ]

A couple of winners from several runs


In [15]:
print evaluate(pid.PID(3.645, 0.0569, -1.1012), 0.5, True)
print evaluate(pid.PID(4.0335, 0.0598, 2.6896), 0.5, True)


ITAE: 570.614013677 MSE: 0.0336216329476 IAE 17.3102745632 PID: [ 3.645 ,  0.0569 ,  -1.1012 ]
570.614013677
ITAE: 594.600432093 MSE: 0.033742696957 IAE 17.5432549039 PID: [ 4.0335 ,  0.0598 ,  2.6896 ]
594.600432093

In [38]:
#MSE driven

evaluate(pid.PID(14.2762504328, 0.100765749933, 11.0081466424), 0.5, True)


ITAE: 634.562287472 MSE: 0.0339330584753 IAE 17.91673891 PID: [ 14.2762504328 ,  0.100765749933 ,  11.0081466424 ]
Out[38]:
634.56228747227965

In [39]:
#IAE driven
print evaluate(pid.PID(7.24791024616, 0.0903963118223, 10.2507944323), 0.5, True)


ITAE: 758.364391895 MSE: 0.0349283203817 IAE 19.2138530154 PID: [ 7.24791024616 ,  0.0903963118223 ,  10.2507944323 ]
758.364391895

And now compare it with the performance of the real plant


In [25]:
t = np.loadtxt("pid-performance2.txt", delimiter=",")
t = t[:,1]
evaluate(pid.PID(23.0048, 0.03763, 31.2907), 0.5, True)
plt.plot(idx(t), t, color="green", label="Real plant")
plt.legend()
plt.figure()


ITAE: 247.043034106 MSE: 0.0327009243653 IAE 14.0407591966 PID: [ 23.0048 ,  0.03763 ,  31.2907 ]
Out[25]:
<matplotlib.figure.Figure at 0x33e0b90>